package com.urick.seq;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class UUIDTester {
    @Test
    public void generatedShortId() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
//        Set<String> uuidSet = new ConcurrentSkipListSet<>();
        List<String> uuidCollection = Collections.synchronizedList(new ArrayList<>());
        ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 16, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        for (int i = 0; i < 100000; i++) {
            executor.execute(()->{
                String uuid = UUIDUtil.shortUuid();
                uuidCollection.add(uuid);
            });
        }
        while (executor.getCompletedTaskCount() != 100000) {
            try {
                countDownLatch.await(3L, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.err.println(uuidCollection.size());
        System.err.println(uuidCollection.stream().distinct().count());
        executor.shutdownNow();
    }

    @Test
    public void nanoId() {
        CountDownLatch countDownLatch = new CountDownLatch(1);
//        Set<String> uuidSet = new ConcurrentSkipListSet<>();
        List<String> uuidCollection = Collections.synchronizedList(new ArrayList<>());
        ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 16, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
        for (int i = 0; i < 1000000; i++) {
            executor.execute(()->{
                uuidCollection.add(NanoIdUtils.randomNanoId());
            });
        }
        while (executor.getCompletedTaskCount() != 1000000) {
            try {
                countDownLatch.await(3L, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.err.println(uuidCollection.size());
        System.err.println(uuidCollection.stream().distinct().count());
        executor.shutdownNow();
    }


    /**
     * d387fa16fb5e46b1941883b8ad3fb6b6
     * d387
     * 54151
     * fa16
     * 64022
     * fb5e
     * 64350
     * 46b1
     * 18097
     * 9418
     * 37912
     * 83b8
     * 33720
     * ad3f
     * 44351
     * b6b6
     * 46774
     * zCUT4Sv0
     */
    @Test
    public void tt2() {
//        System.err.println(UUIDUtil.shortUuid());

//        System.err.println(46774%62);
//        System.err.println(UUID.randomUUID().toString());
        System.err.println(Long.toBinaryString(0x0f));//15
        System.err.println(Long.toBinaryString(0x40));//64
        System.err.println(Long.toBinaryString(0x3f));//63
        System.err.println(Long.toBinaryString(0x80));//128

//        1111
//        1000000
//        111111
//        10000000
    }

    @Test
    public void xx() {
        System.err.println(Math.log(1));
        System.err.println(Math.log(-1));
        System.err.println(Math.log(-2));
        System.err.println(Math.log(0));
        System.err.println(Math.log(Long.MIN_VALUE));
        System.err.println(Math.log(Long.MAX_VALUE));
    }
}
